package com.stylefeng.guns.modular.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.constant.state.Order;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.OrderInfoMapper;
import com.stylefeng.guns.common.persistence.dao.OrderItemMapper;
import com.stylefeng.guns.common.persistence.dao.ProductInfoMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.common.persistence.model.OrderInfo;
import com.stylefeng.guns.common.persistence.model.OrderItem;
import com.stylefeng.guns.common.persistence.model.ProductInfo;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.biz.dao.OrderDao;
import com.stylefeng.guns.modular.biz.service.IOrderService;
import com.stylefeng.guns.modular.biz.warpper.OrderInfoWarpper;
import com.stylefeng.guns.modular.biz.warpper.ProductWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单信息控制器
 *
 * @author fengshuonan
 * @Date 2017-10-17 21:13:36
 */
@Controller
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController {

    private String PREFIX = "/biz/order/";

    @Resource
    private OrderDao orderDao;

    @Resource
    ProductInfoMapper productInfoMapper;

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    IOrderService orderService;

    /**
     * 跳转到订单信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "order.html";
    }

    /**
     * 跳转到添加订单信息
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "order_add.html";
    }

    /**
     * 跳转到修改订单信息
     */
    @RequestMapping("/order_update/{orderId}")
    public String orderUpdate(@PathVariable Integer orderId, Model model) {
        if (ToolUtil.isEmpty(orderId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        OrderInfo  orderInfo=this.orderInfoMapper.selectById(orderId);
        model.addAttribute("order",orderInfo);
        List<OrderItem> list=orderItemMapper.selectListByOrderNo(orderInfo.getOrderNo());
                //.selectList(new EntityWrapper<OrderItem>().eq("order_no", orderInfo.getOrderNo()));
        model.addAttribute("orderItems",list);
        LogObjectHolder.me().set(orderInfo);
        return PREFIX + "order_edit.html";
    }


    /**
     * 获取订单信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(OrderInfo orderInfo) {
        Page<OrderInfo> page = new PageFactory<OrderInfo>().defaultPage();
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        if(ShiroKit.getUser().getDeptId()==27){//超级管理员
            deptid=null;
        }
        List<Map<String, Object>> result = orderDao.getOrders(page ,deptid,orderInfo.getOrderNo(), orderInfo.getUserName() ,
                orderInfo.getUserPhone(),orderInfo.getAddress(),orderInfo.getSetupName(),orderInfo.getSetupName(),orderInfo.getStatus(),
                page.getOrderByField(), page.isAsc()) ;
        page.setRecords((List< OrderInfo>) new OrderInfoWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增订单信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(OrderInfo orderInfo,String orderInfosValues,BindingResult result) {
        if(result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        orderInfo.setDeptid(deptid);
        orderService.addOrderInfo( orderInfo, orderInfosValues);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除订单信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 删除订单item信息
     */
    @RequestMapping(value = "/deleteItem")
    @ResponseBody
    public Object deleteItem(Integer itemId) {
        orderItemMapper.deleteById(itemId);
        return SUCCESS_TIP;
    }


    /**
     * 修改订单信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(OrderInfo orderInfo,String orderInfosValues) {
        System.out.println("orderInfosValuesssss:"+orderInfosValues);
        if(orderInfosValues!=null  &&  !"".equals(orderInfosValues)){
            String [] infos=orderInfosValues.split(",");
            System.out.println("length"+infos.length);
            OrderItem orderItem=null;
            String [] item;
            ProductInfo productInfo;
            for(int i=0;i<infos.length; i++){
                item=infos[i].split("-");
                if( "0".equals(item[4]) ){
                    orderItem=new OrderItem();
                    orderItem.setOrderNo(orderInfo.getOrderNo());
                    orderItem.setProductType(Integer.parseInt(item[0]));
                    orderItem.setProductTypeName(item[1]);
                    orderItem.setPid(Integer.parseInt(item[2]));//产品id
                    productInfo=productInfoMapper.selectById(Integer.parseInt(item[2]));
                    orderItem.setProductModel(productInfo.getProductModel());
                    orderItem.setQuantity(Integer.parseInt(item[3]));
                    //orderItem.setPrice(new Double(item[4]));
                    orderItemMapper.insert(orderItem);
                }else {
                    orderItem=new OrderItem();
                    orderItem.setId(Integer.parseInt(item[4]));
                    orderItem.setProductType(Integer.parseInt(item[0]));
                    orderItem.setProductTypeName(item[1]);
                    orderItem.setPid(Integer.parseInt(item[2]));//产品id
                    productInfo=productInfoMapper.selectById(Integer.parseInt(item[2]));
                    orderItem.setProductModel(productInfo.getProductModel());
                    orderItem.setQuantity(Integer.parseInt(item[3]));
                    //orderItem.setPrice(new Double(item[4]));
                    orderItemMapper.updateById(orderItem);
                }
            }
        }else {
            System.out.println("null");
        }
//        if(orderInfo.getStatus()==1){//预埋
//            if(orderInfo.getIsYumai()==2){//无需预埋
//                orderInfo.setStatus(2);//待装机
//            }
//        }
        orderInfoMapper.updateById(orderInfo);
        return super.SUCCESS_TIP;
    }

    /**
     * 订单信息详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }



}
